/*
 *
 *  Copyright (C) 2010 Tal Eisenberg
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package gui;

import java.awt.GridBagLayout;
import java.awt.event.ContainerEvent;
import java.awt.event.ContainerListener;
import javax.swing.JPanel;
import tal.drivechain.chain.AbortChainException;
import tal.drivechain.gui.GUIContainer;
import tal.drivechain.gui.GUIModule;
import tal.drivechain.plugin.Channel;
import tal.drivechain.variable.Variable;

/**
 *
 * @author eisental
 */
public class Panel extends GUIModule implements GUIContainer {
    String id;

    @Override
    public void setupProperties() throws IllegalArgumentException, AbortChainException {
        component = new JPanel();
        component.setLayout(new GridBagLayout());
        component.addContainerListener(new ContainerListener() {

            public void componentAdded(ContainerEvent e) {
                component.revalidate();
                component.repaint();
            }

            public void componentRemoved(ContainerEvent e) {
                component.revalidate();
                component.repaint();
            }

        });
        super.setupProperties();
    }

    public void setId(String value) {
        if (isRunning())
            throw new IllegalArgumentException("Can't set id while chain is open.");
        if (getParentChain().getSharedObjects().containsValue(component))
            getParentChain().getSharedObjects().remove(id);

        if (value!=null)
            id = (String)value;
        else id = getName();

        getParentChain().getSharedObjects().put(id, component);
    } public String getId() { return id; }

    @Override
    protected void msgReceived(Variable in, long timestamp, Channel channel) {}

    @Override
    public void destroy() {
        super.destroy();
        getParentChain().getSharedObjects().remove(id);
    }

}
